import * as api from './api';
import { dict, UserPageQuery, AddReq, DelReq, EditReq, compute, CreateCrudOptionsProps, CreateCrudOptionsRet } from '@fast-crud/fast-crud';
import { request } from '/@/utils/service';
import { dictionary } from '/@/utils/dictionary';
import { successMessage } from '/@/utils/message';
import {inject} from "vue";
import { useRouter, useRoute } from 'vue-router'
import { log } from 'console';
import { stat } from 'fs';
import { auth, auths, authAll } from '/@/utils/authFunction';


export const createCrudOptions = function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet {
	const pageRequest = async (query: UserPageQuery) => {
		return await api.GetList(query);
	};
	const editRequest = async ({ form, row }: EditReq) => {
		form.id = row.id;
		return await api.UpdateObj(form);
	};
	const delRequest = async ({ row }: DelReq) => {
		return await api.DelObj(row.id);
	};
	const addRequest = async ({ form }: AddReq) => {
		return await api.AddObj(form);
	};

	//权限判定
	// const hasPermissions = inject("$hasPermissions")
	// const hasPermissions = (arg) => {return true}

	const router = useRouter()
	// 工位详情页的按钮回调函数
	const detailLinkButtonHandle =  ({row, $index, index}:any)=>{
		// 从参数中获取row信息
		console.log(row,$index, index,'detail_link clicked....')
		// row.code是工位编码
		const station= row.station
		// console.log('station_code:', station_code)
		// 创建路由对象，用于后面的跳转
		// query 参数用来传参，并且在目标页面接收对应的参数
		const routeObj = {
			path:'/station/station/detail',
			query:{station:station}
		}
		// console.log('routerObj:', routeObj)
		
		router.push(routeObj)
	}

	return {
		crudOptions: {
			request: {
				pageRequest,
				addRequest,
				editRequest,
				delRequest,
			},
			actionbar:{
				// show:hasPermissions("station_station:Create")
				show:auth("station_station:Create")
				// show:false
			},
			rowHandle: {
				//固定右侧
				fixed: 'right',
				width: 150,
				buttons: {
					view: {
						show: false,
					},
					edit: {
						iconRight: 'Edit',
						type: 'text',
						// 只有管理员才能看见
						show:auth("station_station:Update")
					},
					remove: {
						iconRight: 'Delete',
						type: 'text',
						// 只有管理员才能看见
						show:auth("station_station:Delete")
					},
					detail_link:{
						text:'工位详情',
						title:'查看该工位的各类别详细信息',
						type:'primary',
						click:detailLinkButtonHandle,
					}
				},
			},
			form: {
				col: { span: 24 },
				labelWidth: '110px',
				wrapper: {
					is: 'el-dialog',
					width: '600px',
				},
			},
			search:{
				disabled:false,
			},
			columns: {
				_index: {
					title: '序号',
					form: { show: false },
					column: {
						//type: 'index',
						align: 'center',
						width: '70px',
						columnSetDisabled: true, //禁止在列设置中选择
						//@ts-ignore
						formatter: (context) => {
							//计算序号,你可以自定义计算规则，此处为翻页累加
							let index = context.index ?? 1;
							let pagination: any = crudExpose!.crudBinding.value.pagination;
							return ((pagination.currentPage ?? 1) - 1) * pagination.pageSize + index + 1;
						},
					},
				},
				station:{
					title:'工位',
					search:{
						show:true,
						component: {
							props: {
								clearable: true,
							},
							placeholder: '请输入关键词',
						},
					},
				},
				gong_wei_ming_cheng:{
					title:'工位名称',
					search:{
						show:true,
						component: {
							props: {
								clearable: true,
							},
							placeholder: '请输入关键词',
						},
					},
				},
				sheng_chan_xian:{
					title:'生产线',
					search:{
						show:true,
						component: {
							props: {
								clearable: true,
							},
							placeholder: '请输入关键词',
						},
					},
				}

			},
		},
	};
};
